using NFreeMarker.Ext.Util;
using NFreeMarker.Template;
using NFreeMarker.Template.Utility;

namespace NFreeMarker.Ext.Beans
{
    /**
     * Wraps arbitrary subclass of {@link java.lang.Number} into a reflective model.
     * Beside acting as a {@link TemplateNumberModel}, you can call all Java methods on
     * these objects as well.
     */
    public class NumberModel : BeanModel, ITemplateNumberModel
    {
        internal static readonly IModelFactory Factory = new ModelFactoryImpl();

        /**
         * Creates a new model that wraps the specified number object.
         * @param number the number object to wrap into a model.
         * @param wrapper the {@link BeansWrapper} associated with this model.
         * Every model has to have an associated {@link BeansWrapper} instance. The
         * model gains many attributes from its wrapper, including the caching 
         * behavior, method exposure level, method-over-item shadowing policy etc.
         */
        public NumberModel(Number number, BeansWrapper wrapper)
            : base(number, wrapper)
        {
        }

        public Number GetAsNumber()
        {
            return (Number) Object;
        }

        private class ModelFactoryImpl : IModelFactory
        {
            public ITemplateModel Create(object obj, IObjectWrapper wrapper)
            {
                return new NumberModel((Number) obj, (BeansWrapper) wrapper);
            }
        }
    }
}